﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;

namespace EAGL.Explore
{

    public enum LayerType
    {
        ALL, FEATURE_LAYER, RASTER_LAYER
    }
    
    
    // A Helper class to explore to map objects
    public class MapExplorer
    {
        private IMap map;
        
        
#region ============================= PRIVATE ============================

        private List<string> toStringList(List<ILayer> list)
        {
            List<string> slist = new List<string>();
            foreach(ILayer item in list)
            {
                slist.Add(item.Name);
            }
            return slist;
        }
        
        private int getLayerCount(LayerType type)
        {
            return this.map.LayerCount;
        }

        private int getLayerCount()
        {
            return this.map.LayerCount;
        }

        private int getFeatureLayerCount()
        {
            int c = map.LayerCount;
            int x = 0;
            for (int i = 0; i < c; i++)
            {
                ILayer l = map.get_Layer(i);
                if (l is IFeatureLayer)
                {
                    x++;
                }
            }
            return x;
        }

        private int getRasterLayerCount()
        {
            int c = map.LayerCount;
            int x = 0;
            for (int i = 0; i < c; i++)
            {
                ILayer l = map.get_Layer(i);
                if (l is IRasterLayer)
                {
                    x++;
                }
            }
            return x;
        }

        private List<IFeatureLayer> getFeatureLayers()
        {
            int c = map.LayerCount;
            List<IFeatureLayer> layers = new List<IFeatureLayer>();
            for (int i = 0; i < c; i++)
            {
                ILayer l = map.get_Layer(i);
                if (l is IFeatureLayer)
                {
                    layers.Add(l as IFeatureLayer);
                }
            }
            return layers;
        }

        
        private List<IRasterLayer> getRasterLayers()
        {
            int c = map.LayerCount;
            List<IRasterLayer> layers = new List<IRasterLayer>();
            for (int i = 0; i < c; i++)
            {
                ILayer l = map.get_Layer(i);
                if (l is IRasterLayer)
                {
                    layers.Add(l as IRasterLayer);
                }
            }


            return layers;
        }

        private List<ILayer> getLayers()
        {
            int c = map.LayerCount;
            List<ILayer> layers = new List<ILayer>();
            for (int i = 0; i < c; i++)
            {
                ILayer l = map.get_Layer(i);
                layers.Add(l);
            }
            return layers;
        }
#endregion

#region ============================= PUBLIC =============================

        public MapExplorer(IMap map)
        {
            this.map = map;
        }


        public IMap Map
        {
            get { return this.map; }
            set { this.map = value; }
        }

        public List<ILayer> Layers
        {
            get { return getLayers(); }
        }

        public List<IFeatureLayer> FeatureLayers
        {
            get { return getFeatureLayers(); }
        }

        public List<IRasterLayer> RasterLayers
        {
            get { return getRasterLayers(); }
        }

        public int LayerCount
        {
            get {return getLayerCount(); }
        }

        public int FeatureLayerCount
        {
            get { return getFeatureLayerCount(); }
        }

        public int RasterLayerCount
        {
            get { return getRasterLayerCount(); }
        }

        public int GetLayerCount(LayerType type)
        {
            return getLayerCount(type);
        }


        public ILayer GetLayer(int index)
        {
            return map.get_Layer(index);
        }

        public ILayer GetLayer(string name)
        {
            int c = map.LayerCount;
            int i = 0;
            bool found = false;
            ILayer l = null;
            while (i < c && !found)
            {
                l = map.get_Layer(i);
                found = l.Name == name;
                i++;
            }
            if (found)
                return l;
            else
                return null;
        }

        public bool HasLayers
        {
            get { return (getLayerCount() > 0); }
        }

        public bool HasFeatureLayers
        {
            get { return (getFeatureLayerCount() > 0); }
        }

        public bool HasRasterLayers
        {
            get { return (getRasterLayerCount() > 0); }
        }

        public bool IsEmpty
        {
            get { return (getLayerCount() == 0); }
        }



#endregion

    }
}
